import type { Router, RouteRecordRaw } from 'vue-router';

import { PageEnum } from '/@/enums/pageEnum';
import { getTenantId } from '/@/utils/tenant'
const LOGIN_PATH = PageEnum.BASE_LOGIN;
//第三方回调登录路由
const THIRD_LOGIN_PAGE_PATH = PageEnum.THIRD_LOGIN_PAGE_PATH;
//vue3不支持auth2登录
const whitePathList: PageEnum[] = [LOGIN_PATH, THIRD_LOGIN_PAGE_PATH];

export async function createTenantGuard(router: Router) {
  router.beforeEach(async (to, from, next) => {
    if (whitePathList.filter(e => to.path.startsWith(e))?.length > 0) {
      next();
      return
    }
    if ((to.query.tenantId && to.query.appId)) {
      next();
      return;
    };

    let toQuery = JSON.parse(JSON.stringify(to.query));
    toQuery.tenantId = from.query.tenantId || getTenantId();
    toQuery.appId = from.query.appId || '1';
    next({ path: to.path, query: toQuery })
  });
}
